<template>
  <div>
    <!-- 员工列表 -->
    <el-card class="staff_card">
      <el-form :model="searchForm" ref="searchFormRef" inline>
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入姓名"
            class="form_input"
          />
        </el-form-item>
        <el-form-item label="联系方式" prop="phoneNumber">
          <el-input
            v-model="searchForm.phoneNumber"
            placeholder="请输入联系方式"
            class="form_input"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="Search"
            @click="getList()"
            class="btn"
          >
            查询
          </el-button>
          <el-button icon="Refresh" @click="handleReset" class="btn">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt-1">
      <div class="w-full flex justify-end">
        <el-button type="primary" icon="Plus" @click="openDrawer({ id: 0 })"
          >新增</el-button
        >
      </div>

      <el-table :data="tableData" style="height: calc(100vh - 16rem)">
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="address" label="性别">
          <template #default="{ row }">
            {{ row.sex == 1 ? "男" : row.sex == 2 ? "女" : "" }}
          </template>
        </el-table-column>
        <el-table-column label="部门/职位">
          <template #default="{ row }">
            <el-tag
              v-for="item in row.departmentPositionInfo"
              :key="item.id"
              class="mr-1"
            >
              {{
                item.positionName
                  ? item.departmentName + "/" + item.positionName
                  : item.departmentName
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="账号"> </el-table-column>
        <el-table-column prop="idCard" label="身份证号" />
        <el-table-column prop="phoneNumber" label="联系方式" />
        <el-table-column prop="address" label="状态">
          <template #default="{ row }">
            <el-tag type="primary" v-if="row.status == 1">在职</el-tag>
            <el-tag type="danger" v-else>离职</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="openDrawer(row)"
              v-if="row.status == 1"
            >
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button
              type="danger"
              link
              @click="handleDimission(row.id)"
              v-if="row.isInit != 1 && row.status == 1"
            >
              <el-icon><Remove /></el-icon>离职
            </el-button>
            <el-button
              type="danger"
              link
              @click="handleRemove(row.id)"
              v-if="row.status == 2"
            >
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <PagingQuery
        :page="page"
        :pageSize="pageSize"
        :total="total"
        @getList="getList"
        class="mt-1"
      />
    </el-card>

    <el-drawer
      v-model="drawer"
      destroy-on-close
      :title="detailForm.id == 0 ? '新增员工' : '编辑员工'"
    >
      <InsertUpdateStaff
        :detailForm="detailForm"
        :departmentList="departmentList"
        @handleBack="handleBack"
      />
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { cloneDeep } from "lodash";
import PagingQuery from "@/components/PagingQuery.vue";
import InsertUpdateStaff from "./component/InsertUpdateStaff.vue";
import { handleApiResponse } from "@/utils/request.js";
import {
  GetOrganizeEmployeeList,
  ChangeEmployeeStatus,
  DeleteOrganizeEmployeeById,
  GetOrganizeDepartmentTree,
} from "../../../../../wailsjs/go/api/CustomerApi";

onMounted(() => {
  getList();
  getDepartmentList();
});

const searchFormRef = ref(null);
const searchForm = ref({
  name: "",
  phoneNumber: "",
});
const handleReset = () => {
  searchFormRef.value.resetFields();
  getList();
};

const page = ref(1);
const pageSize = ref(10);
const total = ref(0);
const tableData = ref([]);
async function getList(e) {
  if (e) {
    page.value = e.page;
    pageSize.value = e.pageSize;
  }
  handleApiResponse(() =>
    GetOrganizeEmployeeList({
      ...searchForm.value,
      page: page.value,
      pageSize: pageSize.value,
    })
  ).then((res) => {
    if (res.code == 0) {
      tableData.value = res.data.list || [];
      total.value = res.data.total || 0;
    }
  });
}

// 1.新增或者编辑
const drawer = ref(false);
const detailForm = ref({});
const openDrawer = (row) => {
  detailForm.value = cloneDeep(row);
  drawer.value = true;
};
const handleBack = (val) => {
  if (val == 1) {
    getList();
  }
  drawer.value = false;
};

// 离职
const handleDimission = (id) => {
  ElMessageBox.confirm("确定将此员工离职吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    handleApiResponse(() => ChangeEmployeeStatus({ id, status: 2 })).then(
      (res) => {
        if (res.code == 0) {
          ElMessage.success("删除成功");
          getList();
        }
      }
    );
  });
};
// 删除
const handleRemove = (id) => {
  ElMessageBox.confirm("此操作将永久删除该员工, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    handleApiResponse(() => DeleteOrganizeEmployeeById({ id })).then((res) => {
      if (res.code == 0) {
        ElMessage.success("删除成功");
        getList();
      }
    });
  });
};

// 获取部门列表
const departmentList = ref([]);
async function getDepartmentList() {
  handleApiResponse(() => GetOrganizeDepartmentTree()).then((res) => {
    if (res.code == 0) {
      departmentList.value = res.data.departmentTree || [];
    }
  });
}
</script>

<style scoped lang="scss">
.staff_card {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>